#include "glwidget.h"


 GLWidget::GLWidget(QWidget *parent)
     : QGLWidget(parent)
 {
	 setFocusPolicy(Qt::StrongFocus) ;  // Important pour les evenement claviers !!!

	
	 camera = new TrackBallCamera();
	 camera->setrotationSensibilite(25);

     object = 0;
     xRot = 0;
     yRot = 0;
     zRot = 0;

     trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
     trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
 }

 GLWidget::~GLWidget()
 {
     makeCurrent();
     glDeleteLists(object, 1);
 }

 QSize GLWidget::minimumSizeHint() const
 {
     return QSize(50, 50);
 }

 QSize GLWidget::sizeHint() const
 {
     return QSize(1000, 1000);
 }

 void GLWidget::setXRotation(int angle)
 {
     normalizeAngle(&angle);
     if (angle != xRot) {
         xRot = angle;
         emit xRotationChanged(angle);
         updateGL();
     }
 }

 void GLWidget::setYRotation(int angle)
 {
     normalizeAngle(&angle);
     if (angle != yRot) {
         yRot = angle;
         emit yRotationChanged(angle);
         updateGL();
     }
 }

 void GLWidget::setZRotation(int angle)
 {
     normalizeAngle(&angle);
     if (angle != zRot) {
         zRot = angle;
         emit zRotationChanged(angle);
         updateGL();
     }
 }

 void GLWidget::initializeGL()
 {
     qglClearColor(trolltechPurple.dark());
	 glEnable(GL_DEPTH_TEST);
	 glShadeModel(GL_FLAT);
     glEnable(GL_CULL_FACE);
 }

  void GLWidget::resizeGL(int width, int height)
 {
	int side = qMin(width, width);
    glViewport((width - side) / 2, (height - side) / 2, side, side);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70,(float)500/500,1,100);
    
 }

 void GLWidget::paintGL()
 {
 }



 void GLWidget::mousePressEvent(QMouseEvent *event)
 {
	 if (event->buttons() & Qt::MiddleButton) {
         camera->setdistance(2);
		 updateGL();
     }
     lastPos = event->pos();
 }

 void GLWidget::mouseMoveEvent(QMouseEvent *event)
 {
     int dx = event->x() - lastPos.x();
     int dy = event->y() - lastPos.y();
	 if (event->buttons() & Qt::LeftButton) {				//rotation camera sur X et Z

		 if(camera->getfree() != true){
			 camera->setangleX(camera->getangleX() + 8 * dy);
			 camera->setangleY(camera->getangleY() + 8 * dx);
			 updateGL();
		 }else{
			camera->settheta(camera->gettheta()+ dx*4);
			camera->setphi(camera->getphi()- dy*4);
			camera->VectorsFromAngles();
			updateGL();
		 }
     } else if (event->buttons() & Qt::RightButton) {		//rotation camera sur Z et Y
		
		 if(camera->getfree() != true){
			 camera->setangleZ(camera->getangleZ() + 8 * dy);
			 camera->setangleY(camera->getangleY() + 8 * dx);



			 updateGL();
		 }
     
	 }
     lastPos = event->pos();
 }

 void GLWidget::wheelEvent(QWheelEvent *event)
	{
		if(event->delta()>0){

				camera->setdistance(camera->getdistance()+0.4);
				updateGL();
			
		}
		else{
			if(camera->getdistance()>1.9){
				camera->setdistance(camera->getdistance()-0.4);
				updateGL();
			}
		}
		event->ignore();
	}

 void GLWidget::keyPressEvent( QKeyEvent *event )
{
	switch( event ->key() )
	{
	case Qt::Key_Left:
		{
		camera->setangleZ(camera->getangleZ() + 100);
		updateGL();
		break;
		}
	
	case Qt::Key_Right:
		{
		camera->setangleZ(camera->getangleZ() - 100);
		updateGL();
		break;
		}
	case Qt::Key_Up:
		{
			if(camera->getfree()==false){
				camera->setangleX(camera->getangleX() + 100);
				updateGL();
				break;
			}else{
				camera->deplacementHorizontalFreeCam(1);
				updateGL();
				break;
			}
		}
	case Qt::Key_Down:
		{
		if(camera->getfree()==false){
				camera->setangleX(camera->getangleX() - 100);
				updateGL();
				break;
			}else{
				camera->deplacementHorizontalFreeCam(-1);
				updateGL();
				break;
			}
		}
	case Qt::Key_F1:
		{
		camera->setangleX(0);
		camera->setangleY(0);
		camera->setangleZ(0);
		updateGL();
		break;
		}
	case Qt::Key_F5:
		{
		camera->setCamera();
		break;
		}
	case Qt::Key_PageUp:
		{
		camera->deplacementVerticalFreeCam(1);
		updateGL();
		break;
		}
	case Qt::Key_PageDown:
		{
		camera->deplacementVerticalFreeCam(-1);
		updateGL();
		break;
		}
	}
}



 

 void GLWidget::normalizeAngle(int *angle)
 {
     while (*angle < 0)
         *angle += 360 * 16;
     while (*angle > 360 * 16)
         *angle -= 360 * 16;
 }




 
 
 







 

